﻿using System;
using System.Collections.Generic;
using System.Text;
using XProject.DataVisit.Model;
using NHibernate.Criterion;
using NHibernate;
using System.Collections;

namespace XProject.DataVisit.DAL
{
    public class DPagingDal<T> : AbstractSupportDal<T>
        ,IPagingDal<T> where T: IProjectModel
    {
        #region IPagingDal 成员
        /// <summary>
        /// 分页取得全部数据
        /// </summary>
        /// <param name="start"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<T> GetPageData(int start, int pageSize, out long count)
        {
            //List<T> lists = new List<T>();
            IList list =null;
            List<T> lists = new List<T>();
            Template.Execute(delegate(ISession session)
            {
                list = session.CreateMultiCriteria()
                    .Add(session.CreateCriteria(typeof(T)).SetFirstResult(start).SetMaxResults(pageSize))
                    .Add(session.CreateCriteria(typeof(T)).SetProjection(Projections.RowCountInt64())).List();

                foreach (var o in (IList)list[0])
                    lists.Add((T)o);
                return lists;
            });
            count = (long)((IList)list[1])[0];
            return lists;
        }

        #endregion

    }
}
